﻿using MvcPaging;
using Ninject;
using Peninsula.DataAccess.POCO;
using Peninsula.IServices;
using Peninsula.Site.Data.Result;
using Peninsula.Utility.Extensions;
using Peninsula.Web.Areas.Backstage.Models.ItemType;
using Peninsula.Web.Infrastructure;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Peninsula.Web.Areas.Backstage.Controllers
{
    public class ItemTypeController : AdminBaseController
    {
        [Inject]

        public IItemTypeService ItemTypeService { get; set; }

        // GET: Backstage/ItemType
        /// <summary>
        /// 查询维修类型
        /// </summary>
        /// <param name="viewModel"></param>
        /// <returns></returns>
        public ActionResult Index(ItemTypePagedListModel viewModel)
        {
            SetPageTitle("维修类型");
            SetBreadcrumb(parentName: "基本管理");
            IQueryable<ItemType> query = ItemTypeService.Entities;
            if (viewModel.Keyword.IsNotNullAndNotWhiteSpace())
            {
                query = query.Where(s => s.Name.Contains(viewModel.Keyword));
            }
            viewModel.PagedList = query.OrderByDescending(s => s.CreatedTime).ToPagedList(viewModel.PageIndex, viewModel.Size);
            viewModel.QueryParameter = GetParameterFromQueryString();
            return View(viewModel);
        }
        /// <summary>
        /// 编辑或新增维修物品类型
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult CreateOrEditItemType(string id) {
            ItemTypeCreateOrEditModel model = new  ItemTypeCreateOrEditModel();
            ItemType itemType = ItemTypeService.GetById(id);
            model.IsEdit = itemType.IsNotNull();
            if (model.IsEdit)
            {
                model.Id = itemType.Id;
                model.Name = itemType.Name;
            }
            SetPageTitle(model.IsEdit ? "编辑" : "新增" + "维修物品类型");
            SetBreadcrumb(parentName: "基本管理");
            return View(model);
        }

        /// <summary>
        ///  编辑或新增维修物品类型
        /// </summary>
        /// <param name="post"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult CreateOrEditItemType(ItemTypeCreateOrEditModel post) {
            JsonResultData result = new JsonResultData();
            result.RunWithTry(s => {
                if (ModelState.IsValid)
                {
                    bool isSuccess = false;
                    ItemType itemType = ItemTypeService.GetById(post.Id);
                    bool IsEdit = itemType.IsNotNull();
                    itemType = itemType ?? new ItemType();
                    itemType.Name = post.Name;
                    if (ItemTypeService.IsExitItemTyoe(itemType))
                    {
                        s.AddMessage("维修店面已存在！");
                        return;
                    }
                    if (IsEdit)
                    {
                        itemType.SetCreateAndUpdateInfo(AdminSession.AdminUserName);
                        isSuccess = ItemTypeService.Update(itemType) > 0;
                    }
                    else
                    {
                        itemType.SetCreateAndUpdateInfo(AdminSession.AdminUserName);
                        isSuccess = ItemTypeService.Insert(itemType) > 0;
                    }
                    s.Success = isSuccess;
                    s.AddMessage("保存" + (s.Success ? "成功" : "失败"));
                    if (s.Success)
                    {
                        s.RedirectUrl = Url.Action("Index");
                    }
                }
                else
                {
                    s.AddModelValidErrorMessage(ModelState);
                }
            });

            return Json(result);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Delete(string id)
        {
            JsonResultData result = new JsonResultData();
            result.RunWithTry(c =>
            {
                var isDelete = ItemTypeService.DeleteById(id);
                c.Success = isDelete;
                c.AddMessage("删除" + (isDelete ? "成功" : "失败"));
                if (isDelete)
                {
                    c.ReloadPage = true;
                }
            });
            return Json(result);

        }
    }
}